﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GPSTrackingWebsiteManager.Manager.UserControls
{
    public partial class UserSearch : System.Web.UI.UserControl
    {
        //Events
        public delegate void AcceptEvent(Telerik.Web.UI.GridDataItem row);
        public event AcceptEvent OnAcceptEvent;
        public delegate void AddUserEvent(Telerik.Web.UI.GridDataItem row);
        public event AddUserEvent OnAddUserEvent;

        //Propeties
        [Browsable(true), Category("Custom"), DefaultValue(true)]
        public bool IsAddUserVisible { get; set; }

        public void ClearControls()
        {
            txtUserUsername.Text = string.Empty;
            txtUserName.Text = string.Empty;
            txtUserFirstName.Text = string.Empty;
            txtUserLastName.Text = string.Empty;
        }

        public string GetUserUsername()
        {
            return txtUserUsername.Text;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            btnAddUser.Visible = IsAddUserVisible;
        }

        protected void btnUserSearch_Click(object sender, EventArgs e)
        {
            GPSTrackingDBV2.Data.UsersQuery UsersQuery = new GPSTrackingDBV2.Data.UsersQuery();
            if (!string.IsNullOrEmpty(txtSearchUserUsername.Text)) UsersQuery.AppendContains(GPSTrackingDBV2.Entities.UsersColumn.Username, txtSearchUserUsername.Text);
            if (!string.IsNullOrEmpty(txtSearchUserName.Text)) UsersQuery.AppendContains(GPSTrackingDBV2.Entities.UsersColumn.Name, txtSearchUserName.Text);
            if (!string.IsNullOrEmpty(txtSearchUserFirstName.Text)) UsersQuery.AppendContains(GPSTrackingDBV2.Entities.UsersColumn.FirstName, txtSearchUserFirstName.Text);
            if (!string.IsNullOrEmpty(txtSearchUserLastName.Text)) UsersQuery.AppendContains(GPSTrackingDBV2.Entities.UsersColumn.LastName, txtSearchUserLastName.Text);

            UserGrid.DataSource = GPSTrackingDBV2.Data.DataRepository.UsersProvider.Find(UsersQuery);
            UserGrid.DataBind();
        }

        protected void btnUserOK_Click(object sender, EventArgs e)
        {
            if (UserGrid.SelectedItems.Count > 0)
            {
                txtUserUsername.Text = UserGrid.SelectedItems[0].Cells[3].Text;
                txtUserName.Text = UserGrid.SelectedItems[0].Cells[4].Text;
                txtUserFirstName.Text = UserGrid.SelectedItems[0].Cells[5].Text;
                txtUserLastName.Text = UserGrid.SelectedItems[0].Cells[6].Text;

                Telerik.Web.UI.GridDataItem row = UserGrid.SelectedItems[0] as Telerik.Web.UI.GridDataItem;

                if (OnAcceptEvent != null) OnAcceptEvent(row);
            }
        }

        protected void btnAddUser_Click(object sender, EventArgs e)
        {
            if (UserGrid.SelectedItems.Count > 0)
            {
                Telerik.Web.UI.GridDataItem row = UserGrid.SelectedItems[0] as Telerik.Web.UI.GridDataItem;
                OnAddUserEvent(row);
            }
        }
    }
}